

//
//  CategoryViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/2.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryTableViewCell.h"
#import "CategoryModel.h"
#import "PlayerViewController.h"


// 发现-心情 offset * 10   (offset++)

#define MindUrl @"http://bapi.xinli001.com/fm2/broadcast_list.json/?tag=%@&rows=10&key=046b6a2a43dc6ff6e770255f57328f89&offset=%ld&speaker_id=0"

// 发现-场景
#define SceneUrl @"http://bapi.xinli001.com/fm2/broadcast_list.json/?offset=%ld&speaker_id=0&tag=%@&rows=10&key=046b6a2a43dc6ff6e770255f57328f89"


@interface CategoryViewController ()


@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _offset = 0;
    
    self.categoryDataArray = [[NSMutableArray alloc] init];
    
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_myTableView];

    self.navigationItem.title = self.navgiationBarTitle;
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                  NSFontAttributeName:[UIFont systemFontOfSize:15],
                                  NSForegroundColorAttributeName:[UIColor whiteColor]
                                  }];
    
    [self createRefreshHeadView];
    [self createRefreshFootView];
    [self fetchAppDataFromServer];
}

- (void)navigationItemSetting{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)fetchAppDataFromServer{
    //加载提示动画
    /*
     MMProgressHUDPresentationStyleDrop = 0, //default
     MMProgressHUDPresentationStyleExpand,
     MMProgressHUDPresentationStyleShrink,
     MMProgressHUDPresentationStyleSwingLeft,
     MMProgressHUDPresentationStyleSwingRight,
     MMProgressHUDPresentationStyleBalloon,
     MMProgressHUDPresentationStyleFade,
     MMProgressHUDPresentationStyleNone

     */
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleBalloon];
//    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleDrop];
    [MMProgressHUD showWithTitle:@"努力加载中..."];

    self.urlStr = [self composeServerUrl];

    
    [[NetDataEngine sharedInstance] requestDiscoverUrl:self.urlStr success:^(id respondObject) {
        //加载成功
        [MMProgressHUD dismissWithSuccess:@"加载成功!"];
        //成功后解析数据
        if (self.offset == 0) {
            [self.categoryDataArray removeAllObjects];
        }
        [self.categoryDataArray addObjectsFromArray:[CategoryModel parseRespondsData:respondObject]];
        //刷表
        [self.myTableView reloadData];
        //停止刷新
        [self endRefreshing];
        
    } failed:^(NSError *error) {
        
        [MMProgressHUD dismissWithError:@"记载数据失败!"];
        //
        NSLog(@"%@", error);
        
        [self endRefreshing];
    }];
    
}


- (NSString *)composeServerUrl{
    
    NSString *str = [[NSString alloc] init];
        if (self.cellSection == 0) {
            str = [NSString stringWithFormat:CategoryGrow, self.cellTag - 9, self.offset*10];

        }else if (self.cellSection == 1){
//            str = [NSString stringWithFormat:MindUrl, [self.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], self.offset*10];
            str = [NSString stringWithFormat:MindUrl, self.name, self.offset*10];
    
        }else if (self.cellSection == 2){
//            str = [NSString stringWithFormat:SceneUrl, self.offset*10, [self.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            str = [NSString stringWithFormat:SceneUrl, self.offset*10, self.name];
        }
    return str;
}



#pragma mark - UITableViewDataSource
//返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
//每组返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.categoryDataArray.count;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (self.categoryDataArray == nil) {
            return nil;
        }
        
        static NSString *cellID0 = @"cellID0";
        CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID0];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CategoryTableViewCell" owner:self options:nil] lastObject];
        }
    
    cell.contentView.backgroundColor = [UIColor colorWithRed:20/255.0 green:20/255.0 blue:20/255.0 alpha:1.0];
    CategoryModel *model = self.categoryDataArray[indexPath.row];
    [cell updateWithModel:model];
    return cell;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}


#pragma mark - 选中 UICollectionView 中某一个item
//选中某一个item

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//----------------------------加入播放列表动画--------------------
    CategoryModel *model = self.categoryDataArray[indexPath.row];
    CategoryTableViewCell *cell = (CategoryTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    CGFloat y = cell.frame.origin.y - tableView.contentOffset.y;
    [LZXHelper animationPlayWithUrl:model.cover viewController:self leaderFrame:CGRectMake(8, y+64+7, 50, 50) newFrame:CGRectMake(self.view.bounds.size.width-30, 30, 6, 6)];
    
    [PlayerViewController sharePlayer].mainImageUrl = model.cover;
    [PlayerViewController sharePlayer].topicTitle = model.title;
    [PlayerViewController sharePlayer].viewNum = model.viewnum;
    [PlayerViewController sharePlayer].avaImageUrl = model.cover;
    [PlayerViewController sharePlayer].speakNickName = model.speak;
    
    [[PlayerViewController sharePlayer] playWithUrl:model.url];
}

#pragma mark - 刷新
- (void)createRefreshHeadView{
    __weak typeof(self) wSelf = self;
    [self.myTableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isRefreshing) {
            return ;
        }
        wSelf.isRefreshing = YES;
        [wSelf initParamater];
        [wSelf fetchAppDataFromServer];
    }];
}

- (void)createRefreshFootView{
    __weak typeof(self) wSelf = self;
    [self.myTableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isLoadingMore) {
            return ;
        }
        wSelf.isLoadingMore = YES;
        wSelf.offset++;
        [wSelf fetchAppDataFromServer];
    }];
    
}

- (void)endRefreshing{
    if (self.isRefreshing) {
        [self.myTableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
        self.isRefreshing = NO;
    }
    if (self.isLoadingMore) {
        self.isLoadingMore = NO;
        [self.myTableView footerEndRefreshing];
    }
}

- (void)initParamater{
    self.category_id = 1;
    self.offset = 0;
}


@end
